/*
 * Client.cpp
 *
 *  Created on: 8 mei 2013
 *      Author: maarten
 */


#include "Client.hpp"
#include "ClientCommands.hpp"

#include <iostream>
#include <cstdlib>			//exit
#include <cstdio>			//perror
#include <cstring>			//memset
#include <unistd.h>			//close
#include <arpa/inet.h>		//inetaddr

#include <dirent.h>     //dir etc.

int main(int argc, char **argv) {
	Client c = Client();
}

Client::Client() {
	char dir[255];
	getcwd(dir, 255);
	std::vector<std::string> dirStructure = getDirStructure(dir);

	do {
		setConnectionDetails();
	} while (createConnection() == -1);
	ClientCommands cc = ClientCommands(socketDescriptor);

	do {
		setUserDetails();
	} while (cc.requestLogon(username, password) == -1);

	//cc.requestUpload("/home/ken/Desktop/testpng.png");
	//cc.requestUpload("/home/ken/Desktop/veel_data");
	cc.requestUpload("/home/ken/Desktop/testdata.txt");
	//cc.requestUpload("/home/ken/Dropbox/Sem-MPNA/test.pdf");

	//cc.requestDownload("veel_data");
	cc.requestDownload("testdata.txt");
	//cc.requestDownload("tekst2.txt");
	//cc.requestDownload("testpng.png");


	cc.requestDirStructure("");
}

std::vector<std::string> Client::getDirStructure (char dirName[255]) {
	DIR* dir;
	struct dirent *dirEntry;
	dir = opendir(dirName);

	std::vector<std::string> dirStructure;
	std::string dirstring;
	std::string filestring;

	while ((dirEntry = readdir(dir)) != NULL) {

		if (dirEntry->d_name[0] != '.') {
			if (dirEntry->d_type == 4) {
				dirstring += dirEntry->d_name;
				dirstring += ", ";
			} else if (dirEntry->d_type == 8) {
				filestring += dirEntry->d_name;
				filestring += ", ";
			}
		}
	}
	dirStructure.push_back(dirstring);
	dirStructure.push_back(filestring);
	return dirStructure;
}

Client::~Client() {
}

/*
 * asks the user to give the server IP and port.
 */
void Client::setConnectionDetails() {
	std::cout << "IP:\n>";
	std::getline(std::cin, ip);

	std::cout << "Port:\n>";
	std::getline(std::cin, port);
}

/*
 * asks the user to give his username and password to login for the server.
 */
void Client::setUserDetails() {
	std::cout << "Username:\n>";
	std::getline(std::cin, username);

	std::cout << "Password:\n>";
	std::getline(std::cin, password);
}

/*
 * create and fill the sockaddr_in.
 * make a socketdescriptor and connect to the server.
 */
int Client::createConnection() {
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;

	//check ip
	if ((signed) (addr.sin_addr.s_addr = inet_addr(ip.c_str())) == -1) {
		std::cout << "Ip is not valid" << std::endl;
		return(-1);
	}

	//check port
	if ((addr.sin_port = htons(std::atoi(port.c_str()))) == 0) {
		std::cout << "Port is not valid" << std::endl;
		return(-1);
	}

	//make socket
	if ((socketDescriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		std::perror("Cannot make socket");
		return(-1);
	}

	//connect to socket
	if ((connect(socketDescriptor, (struct sockaddr*) &addr, sizeof(addr))) != 0) {
		perror("Can't connect");
		return(-1);
	}
	return 0;
}
